package com.example.common.interpretor;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
/**
 * 请求拦截器，在restTemplate请求中自动加入token
 */

public class AuthorizationHeaderInterceptor implements ClientHttpRequestInterceptor {
    private final String token;

    public AuthorizationHeaderInterceptor(String token) {
        this.token = token;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        // 添加Authorization头
        request.getHeaders().add("Authorization", "Bearer " + this.token);
        // 如果需要，还可以添加其他请求头
        // request.getHeaders().add("Another-Header", "HeaderValue");
        return execution.execute(request, body);
    }
}
